/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logica.jpaControllers;

import Entidades.Modulo;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entidades.Rolmodulo;
import java.util.ArrayList;
import java.util.Collection;
import Entidades.Rolareamodulo;
import Entidades.Submodulo;
import logica.jpaControllers.exceptions.IllegalOrphanException;
import logica.jpaControllers.exceptions.NonexistentEntityException;

/**
 *
 * @author Anali
 */
public class ModuloJpaController {

    public ModuloJpaController() {
        emf = Persistence.createEntityManagerFactory("GestorPesajeJpaPU2");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Modulo modulo) {
        if (modulo.getRolmoduloCollection() == null) {
            modulo.setRolmoduloCollection(new ArrayList<Rolmodulo>());
        }
        if (modulo.getRolareamoduloCollection() == null) {
            modulo.setRolareamoduloCollection(new ArrayList<Rolareamodulo>());
        }
        if (modulo.getSubmoduloCollection() == null) {
            modulo.setSubmoduloCollection(new ArrayList<Submodulo>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Rolmodulo> attachedRolmoduloCollection = new ArrayList<Rolmodulo>();
            for (Rolmodulo rolmoduloCollectionRolmoduloToAttach : modulo.getRolmoduloCollection()) {
                rolmoduloCollectionRolmoduloToAttach = em.getReference(rolmoduloCollectionRolmoduloToAttach.getClass(), rolmoduloCollectionRolmoduloToAttach.getIdRolModulo());
                attachedRolmoduloCollection.add(rolmoduloCollectionRolmoduloToAttach);
            }
            modulo.setRolmoduloCollection(attachedRolmoduloCollection);
            Collection<Rolareamodulo> attachedRolareamoduloCollection = new ArrayList<Rolareamodulo>();
            for (Rolareamodulo rolareamoduloCollectionRolareamoduloToAttach : modulo.getRolareamoduloCollection()) {
                rolareamoduloCollectionRolareamoduloToAttach = em.getReference(rolareamoduloCollectionRolareamoduloToAttach.getClass(), rolareamoduloCollectionRolareamoduloToAttach.getIdRolAreaModulo());
                attachedRolareamoduloCollection.add(rolareamoduloCollectionRolareamoduloToAttach);
            }
            modulo.setRolareamoduloCollection(attachedRolareamoduloCollection);
            Collection<Submodulo> attachedSubmoduloCollection = new ArrayList<Submodulo>();
            for (Submodulo submoduloCollectionSubmoduloToAttach : modulo.getSubmoduloCollection()) {
                submoduloCollectionSubmoduloToAttach = em.getReference(submoduloCollectionSubmoduloToAttach.getClass(), submoduloCollectionSubmoduloToAttach.getIdSubModulo());
                attachedSubmoduloCollection.add(submoduloCollectionSubmoduloToAttach);
            }
            modulo.setSubmoduloCollection(attachedSubmoduloCollection);
            em.persist(modulo);
            for (Rolmodulo rolmoduloCollectionRolmodulo : modulo.getRolmoduloCollection()) {
                Modulo oldModuloOfRolmoduloCollectionRolmodulo = rolmoduloCollectionRolmodulo.getModulo();
                rolmoduloCollectionRolmodulo.setModulo(modulo);
                rolmoduloCollectionRolmodulo = em.merge(rolmoduloCollectionRolmodulo);
                if (oldModuloOfRolmoduloCollectionRolmodulo != null) {
                    oldModuloOfRolmoduloCollectionRolmodulo.getRolmoduloCollection().remove(rolmoduloCollectionRolmodulo);
                    oldModuloOfRolmoduloCollectionRolmodulo = em.merge(oldModuloOfRolmoduloCollectionRolmodulo);
                }
            }
            for (Rolareamodulo rolareamoduloCollectionRolareamodulo : modulo.getRolareamoduloCollection()) {
                Modulo oldModuloOfRolareamoduloCollectionRolareamodulo = rolareamoduloCollectionRolareamodulo.getModulo();
                rolareamoduloCollectionRolareamodulo.setModulo(modulo);
                rolareamoduloCollectionRolareamodulo = em.merge(rolareamoduloCollectionRolareamodulo);
                if (oldModuloOfRolareamoduloCollectionRolareamodulo != null) {
                    oldModuloOfRolareamoduloCollectionRolareamodulo.getRolareamoduloCollection().remove(rolareamoduloCollectionRolareamodulo);
                    oldModuloOfRolareamoduloCollectionRolareamodulo = em.merge(oldModuloOfRolareamoduloCollectionRolareamodulo);
                }
            }
            for (Submodulo submoduloCollectionSubmodulo : modulo.getSubmoduloCollection()) {
                Modulo oldModuloOfSubmoduloCollectionSubmodulo = submoduloCollectionSubmodulo.getModulo();
                submoduloCollectionSubmodulo.setModulo(modulo);
                submoduloCollectionSubmodulo = em.merge(submoduloCollectionSubmodulo);
                if (oldModuloOfSubmoduloCollectionSubmodulo != null) {
                    oldModuloOfSubmoduloCollectionSubmodulo.getSubmoduloCollection().remove(submoduloCollectionSubmodulo);
                    oldModuloOfSubmoduloCollectionSubmodulo = em.merge(oldModuloOfSubmoduloCollectionSubmodulo);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Modulo modulo) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Modulo persistentModulo = em.find(Modulo.class, modulo.getIdModulo());
            Collection<Rolmodulo> rolmoduloCollectionOld = persistentModulo.getRolmoduloCollection();
            Collection<Rolmodulo> rolmoduloCollectionNew = modulo.getRolmoduloCollection();
            Collection<Rolareamodulo> rolareamoduloCollectionOld = persistentModulo.getRolareamoduloCollection();
            Collection<Rolareamodulo> rolareamoduloCollectionNew = modulo.getRolareamoduloCollection();
            Collection<Submodulo> submoduloCollectionOld = persistentModulo.getSubmoduloCollection();
            Collection<Submodulo> submoduloCollectionNew = modulo.getSubmoduloCollection();
            List<String> illegalOrphanMessages = null;
            for (Rolmodulo rolmoduloCollectionOldRolmodulo : rolmoduloCollectionOld) {
                if (!rolmoduloCollectionNew.contains(rolmoduloCollectionOldRolmodulo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Rolmodulo " + rolmoduloCollectionOldRolmodulo + " since its modulo field is not nullable.");
                }
            }
            for (Rolareamodulo rolareamoduloCollectionOldRolareamodulo : rolareamoduloCollectionOld) {
                if (!rolareamoduloCollectionNew.contains(rolareamoduloCollectionOldRolareamodulo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Rolareamodulo " + rolareamoduloCollectionOldRolareamodulo + " since its modulo field is not nullable.");
                }
            }
            for (Submodulo submoduloCollectionOldSubmodulo : submoduloCollectionOld) {
                if (!submoduloCollectionNew.contains(submoduloCollectionOldSubmodulo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Submodulo " + submoduloCollectionOldSubmodulo + " since its modulo field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Rolmodulo> attachedRolmoduloCollectionNew = new ArrayList<Rolmodulo>();
            for (Rolmodulo rolmoduloCollectionNewRolmoduloToAttach : rolmoduloCollectionNew) {
                rolmoduloCollectionNewRolmoduloToAttach = em.getReference(rolmoduloCollectionNewRolmoduloToAttach.getClass(), rolmoduloCollectionNewRolmoduloToAttach.getIdRolModulo());
                attachedRolmoduloCollectionNew.add(rolmoduloCollectionNewRolmoduloToAttach);
            }
            rolmoduloCollectionNew = attachedRolmoduloCollectionNew;
            modulo.setRolmoduloCollection(rolmoduloCollectionNew);
            Collection<Rolareamodulo> attachedRolareamoduloCollectionNew = new ArrayList<Rolareamodulo>();
            for (Rolareamodulo rolareamoduloCollectionNewRolareamoduloToAttach : rolareamoduloCollectionNew) {
                rolareamoduloCollectionNewRolareamoduloToAttach = em.getReference(rolareamoduloCollectionNewRolareamoduloToAttach.getClass(), rolareamoduloCollectionNewRolareamoduloToAttach.getIdRolAreaModulo());
                attachedRolareamoduloCollectionNew.add(rolareamoduloCollectionNewRolareamoduloToAttach);
            }
            rolareamoduloCollectionNew = attachedRolareamoduloCollectionNew;
            modulo.setRolareamoduloCollection(rolareamoduloCollectionNew);
            Collection<Submodulo> attachedSubmoduloCollectionNew = new ArrayList<Submodulo>();
            for (Submodulo submoduloCollectionNewSubmoduloToAttach : submoduloCollectionNew) {
                submoduloCollectionNewSubmoduloToAttach = em.getReference(submoduloCollectionNewSubmoduloToAttach.getClass(), submoduloCollectionNewSubmoduloToAttach.getIdSubModulo());
                attachedSubmoduloCollectionNew.add(submoduloCollectionNewSubmoduloToAttach);
            }
            submoduloCollectionNew = attachedSubmoduloCollectionNew;
            modulo.setSubmoduloCollection(submoduloCollectionNew);
            modulo = em.merge(modulo);
            for (Rolmodulo rolmoduloCollectionNewRolmodulo : rolmoduloCollectionNew) {
                if (!rolmoduloCollectionOld.contains(rolmoduloCollectionNewRolmodulo)) {
                    Modulo oldModuloOfRolmoduloCollectionNewRolmodulo = rolmoduloCollectionNewRolmodulo.getModulo();
                    rolmoduloCollectionNewRolmodulo.setModulo(modulo);
                    rolmoduloCollectionNewRolmodulo = em.merge(rolmoduloCollectionNewRolmodulo);
                    if (oldModuloOfRolmoduloCollectionNewRolmodulo != null && !oldModuloOfRolmoduloCollectionNewRolmodulo.equals(modulo)) {
                        oldModuloOfRolmoduloCollectionNewRolmodulo.getRolmoduloCollection().remove(rolmoduloCollectionNewRolmodulo);
                        oldModuloOfRolmoduloCollectionNewRolmodulo = em.merge(oldModuloOfRolmoduloCollectionNewRolmodulo);
                    }
                }
            }
            for (Rolareamodulo rolareamoduloCollectionNewRolareamodulo : rolareamoduloCollectionNew) {
                if (!rolareamoduloCollectionOld.contains(rolareamoduloCollectionNewRolareamodulo)) {
                    Modulo oldModuloOfRolareamoduloCollectionNewRolareamodulo = rolareamoduloCollectionNewRolareamodulo.getModulo();
                    rolareamoduloCollectionNewRolareamodulo.setModulo(modulo);
                    rolareamoduloCollectionNewRolareamodulo = em.merge(rolareamoduloCollectionNewRolareamodulo);
                    if (oldModuloOfRolareamoduloCollectionNewRolareamodulo != null && !oldModuloOfRolareamoduloCollectionNewRolareamodulo.equals(modulo)) {
                        oldModuloOfRolareamoduloCollectionNewRolareamodulo.getRolareamoduloCollection().remove(rolareamoduloCollectionNewRolareamodulo);
                        oldModuloOfRolareamoduloCollectionNewRolareamodulo = em.merge(oldModuloOfRolareamoduloCollectionNewRolareamodulo);
                    }
                }
            }
            for (Submodulo submoduloCollectionNewSubmodulo : submoduloCollectionNew) {
                if (!submoduloCollectionOld.contains(submoduloCollectionNewSubmodulo)) {
                    Modulo oldModuloOfSubmoduloCollectionNewSubmodulo = submoduloCollectionNewSubmodulo.getModulo();
                    submoduloCollectionNewSubmodulo.setModulo(modulo);
                    submoduloCollectionNewSubmodulo = em.merge(submoduloCollectionNewSubmodulo);
                    if (oldModuloOfSubmoduloCollectionNewSubmodulo != null && !oldModuloOfSubmoduloCollectionNewSubmodulo.equals(modulo)) {
                        oldModuloOfSubmoduloCollectionNewSubmodulo.getSubmoduloCollection().remove(submoduloCollectionNewSubmodulo);
                        oldModuloOfSubmoduloCollectionNewSubmodulo = em.merge(oldModuloOfSubmoduloCollectionNewSubmodulo);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = modulo.getIdModulo();
                if (findModulo(id) == null) {
                    throw new NonexistentEntityException("The modulo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Modulo modulo;
            try {
                modulo = em.getReference(Modulo.class, id);
                modulo.getIdModulo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The modulo with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Rolmodulo> rolmoduloCollectionOrphanCheck = modulo.getRolmoduloCollection();
            for (Rolmodulo rolmoduloCollectionOrphanCheckRolmodulo : rolmoduloCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Modulo (" + modulo + ") cannot be destroyed since the Rolmodulo " + rolmoduloCollectionOrphanCheckRolmodulo + " in its rolmoduloCollection field has a non-nullable modulo field.");
            }
            Collection<Rolareamodulo> rolareamoduloCollectionOrphanCheck = modulo.getRolareamoduloCollection();
            for (Rolareamodulo rolareamoduloCollectionOrphanCheckRolareamodulo : rolareamoduloCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Modulo (" + modulo + ") cannot be destroyed since the Rolareamodulo " + rolareamoduloCollectionOrphanCheckRolareamodulo + " in its rolareamoduloCollection field has a non-nullable modulo field.");
            }
            Collection<Submodulo> submoduloCollectionOrphanCheck = modulo.getSubmoduloCollection();
            for (Submodulo submoduloCollectionOrphanCheckSubmodulo : submoduloCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Modulo (" + modulo + ") cannot be destroyed since the Submodulo " + submoduloCollectionOrphanCheckSubmodulo + " in its submoduloCollection field has a non-nullable modulo field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(modulo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Modulo> findModuloEntities() {
        return findModuloEntities(true, -1, -1);
    }

    public List<Modulo> findModuloEntities(int maxResults, int firstResult) {
        return findModuloEntities(false, maxResults, firstResult);
    }

    private List<Modulo> findModuloEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Modulo as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Modulo findModulo(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Modulo.class, id);
        } finally {
            em.close();
        }
    }

    public int getModuloCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Modulo as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
     public Modulo findModulo_x_nombre(String nom) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select u from Modulo u where u.nombreModulo = " + "'" + nom +"'");
            return (Modulo) q.getSingleResult();
        } finally {
            em.close();
        }
    }

    public List<Modulo> findModuloEntities(String nom) {
        EntityManager em = getEntityManager();
        try {
            //Query q = em.createNamedQuery("Modulo.findByNombreModulo");
            //q.setParameter("nomMod", nom);
            Query q = em.createQuery("select u from Modulo u where u.nombreModulo = " + "'" + nom +"'");
            return q.getResultList();
        } finally {
            em.close();
        }
    }

}
